package Bulders;
//Builder pattern
/**
 * 保证重叠构造器的安全，也能保证像javabean那样的可读
 * @author ctk
 * 在设计这个类的时候如果有多种可选参数，并且是准备设计成静态工厂的情况下
 */
public class NutritionFacts {
	public final int arg1;
	public final int arg2;
	public final int arg3;
	//builder
	public static class Builder{
		private final int arg1;
		
		private int arg2;
		private int arg3;
		public Builder(int arg1){
			this.arg1 = arg1;
		}
		public Builder setArg2(int val){
			this.arg2 = val;
			return this;
		}
		public Builder setArg3(int val){
			this.arg3 = val;
			return this;
		}
		//这个方法返回实例
		public NutritionFacts build(){
			return new NutritionFacts(this);
		}
	}
	private NutritionFacts(Builder builder){
		arg1 = builder.arg1;
		arg2 = builder.arg2;
		arg3 = builder.arg3;
	}
	public static void main(String[] args) {
		//客户端代码
		//在setter里面添加约束条件
		//如果不合法就直接返回不会等到builder
		NutritionFacts test = new NutritionFacts.Builder(1).setArg2(1).setArg3(1).build();
		System.out.println(test.arg1);
	}
}
